<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>vue</title>
  <script src="../js/vue.js"></script>
  <script src="../js/vue-router.js"></script>
</head>
<body>
  <div id="app">
    <!-- 4.roter使用 跳转链接 加载组件的区域 -->
    <!-- <router-link to="/index">点击跳转首页</router-link>
    <router-link to="/a">点击跳转A</router-link>
    <router-link to="/b">点击跳转B</router-link> -->
    <button @click="clickHandler">首页</button>
    <button @click="clickHandler">A页面</button>
    <button @click="clickHandler">B页面</button>
    <div>
      <!-- 加载组件的区域 -->
      <router-view></router-view>
    </div>
  </div>
  <script>
    // 1.组件定义
    let ComponentIndex = {
      template: `
        <div>
          <router-link to="/index/category">栏目管理</router-link>  
          <router-link to="/index/article">文章管理</router-link>
          <div>
            <router-view></router-view>  
          </div>  
        </div>
      `
    }
    let ComponentA = {
      template: `
        <div>组件A内容</div>
      `,
      created() {
        console.log(this.$route);
      }
    }
    let ComponentB = {
      template: `
        <div>组件B内容/
          <button @click="$router.go(-1)">返回</button>  
        </div>
      `,
      created() {
        console.log(this.$route);
      }
    }
    let Category = {
      template: `
        <div>栏目管理组件</div>
      `
    }
    let Article = {
      template: `
        <div>文章管理组件</div>
      `
    }
    // 2.路由的定义
    let router = new VueRouter({
      // mode: 'hash',
      mode: 'history',
      routes:[
        {
          path: '/index',
          name: 'index',
          component: ComponentIndex,
          children: [
            //       /index/category
            { path: 'category', component: Category },
            { path: 'article', component: Article },
          ]
        },
        {
          path: '/a',
          component: ComponentA
        },
        {
          path: '/b',
          name: 'bRoute',
          component: ComponentB
        },
      ]
    })
    let vm = new Vue({
      el: '#app',
      // 3.组件内注册路由
      router: router,
      data: {

      },
      methods: {
        // 按钮点击处理函数
        clickHandler(e) {
          // console.log(e.target.innerText);
          switch (e.target.innerText) {
            case '首页':
              // this.$router.push('/index');
              // this.$router.push({ path: '/index' });
              this.$router.push({ name: 'index' });
              break;
            case 'A页面':
              this.$router.push({ path: '/a', query: {id:1,name:'张三'}});
              break;
            case 'B页面':
              // this.$router.push('/b');
              this.$router.push({ name: 'bRoute', params:{id:2,name:'李四'}});
              break;
          }
        }
      }
    })
  </script>
</body>
</html>